﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace Blackbird.Dev.Utility
{
    /// <summary>
    /// Interaction logic for SelectDirectoryDialog.xaml
    /// </summary>
    public partial class SelectDirectoryDialog
    {
        public SelectDirectoryDialog()
        {
            InitializeComponent();
            InitialDirectory = "C:/";
        }

        public string InitialDirectory { get; set; }
        public string SelectedDirectory { get; set; }

        private void WindowActivated(object sender, EventArgs e)
        {
            Activated -= WindowActivated;
            DirectoryTree.Items.Clear();
            InitialDirectory = Path.GetFullPath(InitialDirectory);
            string[] splitOffDrive = InitialDirectory.Split(new[] {':'});
            string drive = splitOffDrive[0];
            string[] pathParts = splitOffDrive[1].Split(new[] {'/', '\\'});

            if (Directory.Exists(InitialDirectory))
            {
                string runningDirectory = string.Concat(drive, ":");
                var currentItem = new TreeViewItem
                                      {
                                          Header = runningDirectory,
                                          ToolTip = runningDirectory
                                      };
                DirectoryTree.Items.Add(currentItem);

                foreach (string part in pathParts)
                {
                    var item = new TreeViewItem
                                   {
                                       Header = part,
                                       ToolTip = runningDirectory
                                   };

                    runningDirectory = string.Concat(runningDirectory, "/", part);
                    currentItem.Items.Add(item);
                    currentItem = item;
                }
                LoadNode(currentItem, InitialDirectory, true);
            }
            else
                throw
                    new DirectoryNotFoundException(string.Format("The requested directory was not found: {0}",
                                                                 InitialDirectory));
        }


        private void LoadNode(TreeViewItem node, string parentDirectory, bool recusive)
        {
            node.Items.Clear();
            string[] directories = Directory.GetDirectories(parentDirectory);
            foreach (string directory in directories)
            {
                var item = new TreeViewItem
                               {
                                   Header = PathTip(directory.Replace('\\', '/')),
                                   ToolTip = directory
                               };

                item.Expanded += ItemExpanded;
                node.Items.Add(item);
                if (recusive)
                    LoadNode(item, directory, false);
            }
        }

        private void ItemExpanded(object sender, RoutedEventArgs e)
        {
            var item = (TreeViewItem) sender;
            if (item == e.Source)
                foreach (TreeViewItem child in item.Items)
                    LoadNode(child, child.ToolTip.ToString(), true);
        }

        private string PathTip(string fullDirectory)
        {
            string trimmedDirectory = fullDirectory;
            while (trimmedDirectory.EndsWith("/"))
            {
                trimmedDirectory = trimmedDirectory.Substring(0, trimmedDirectory.Length - 1);
            }

            int lastSeperatorIndex = trimmedDirectory.LastIndexOf('/') + 1;
            if (lastSeperatorIndex > 0)
            {
                trimmedDirectory = trimmedDirectory.Substring(lastSeperatorIndex);
            }
            return trimmedDirectory;
        }
    }
}